package com.hengxinguotong.zhihuichengjian.ui.engineering.task;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.StructureRes;
import com.hengxinguotong.zhihuichengjian.bean.StructureBean;
import com.hengxinguotong.zhihuichengjian.treeview.Node;
import com.hengxinguotong.zhihuichengjian.treeview.NodeHelper;
import com.hengxinguotong.zhihuichengjian.treeview.NodeTreeAdapter;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @CopyRight 恒信国通信息科技
 * @Author wulei
 * @Date 2018/4/12
 *
 * 工程结构
 */

public class ProjectStructureActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.project_list)
    ListView projectList;
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    private LinkedList<Node> mLinkedList = new LinkedList<>();
    private NodeTreeAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project_structure);
        ButterKnife.bind(this);
        titleTv.setText("工程结构");
        mAdapter = new NodeTreeAdapter(this,projectList,mLinkedList);
        projectList.setAdapter(mAdapter);
        getStructureInfo(null);
    }

    private void getStructureInfo(String id) {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("constructionId", SPUtil.getString(this,"constructionid"));
            if(!Utils.isEmpty(id)){
                jsonObject.put("id", id);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData(this, "/projectStructure/loadTree/" + SPUtil.getString(this,"uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {

                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    StructureRes structureRes = gson.fromJson(json, StructureRes.class);
                    if(structureRes.getStatus() == 1){
                        List<StructureBean> structureBeanList = structureRes.getValue();
                        if(structureBeanList.size() <= 0){
                            showToast("当前列表数据为空");
                        }else{
                            List<Node> data = addData(structureRes.getValue());
                            mLinkedList.addAll(NodeHelper.sortNodes(data));
                            mAdapter.notifyDataSetChanged();
                        }
                    }else{
                        showToast(structureRes.getMsg());
                    }
                }
            }

            @Override
            public void onFailure(String json) {
            }
        });
    }

    private List<Node> addData(List<StructureBean> datas){
        List<Node> data = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            data.add(datas.get(i));
        }
        return data;
    }

    @OnClick({R.id.iv_back})
    public void onClick(View v) {
        finish();
    }
}
